package com.twxm.breed.service;

import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsNotNull;
import com.twxm.breed.entity.FishPond;
import com.twxm.breed.entity.WaterFactor;
import com.twxm.breed.entity.WeatherFactor;
import com.twxm.breed.mapper.FishPondMapper;
import com.twxm.breed.mapper.WeatherFactorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WeatherFactorService {
    @Autowired
    private WeatherFactorMapper weatherFactorMapper;

    @Autowired
    private FishPondMapper fishPondMapper;

    @Transactional(rollbackFor = Exception.class)
    public void insertList(List<HashMap<String,String>> list) throws Exception {
        for (int i = 0; i < list.size(); ++i) {
            Date date = new Date();
            float temperature = 0;
            float windSpeed = 0;
            float airPressure = 0;
            float illumination = 0;
            float humidity = 0;
            String pondId = "";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            for (String key : list.get(i).keySet()) {
                if (key.equals("气温")||key.equals("temperature")) {
                    temperature = Float.parseFloat(list.get(i).get(key));
                } else if (key.equals("风速")||key.equals("windSpeed")) {
                    windSpeed = Float.parseFloat(list.get(i).get(key));
                } else if (key.equals("气压")||key.equals("airPressure")) {
                    airPressure = Float.parseFloat(list.get(i).get(key));
                } else if (key.equals("光照")||key.equals("illumination")) {
                    illumination = Float.parseFloat(list.get(i).get(key));
                } else if (key.equals("湿度")||key.equals("humidity")) {
                    humidity = Float.parseFloat(list.get(i).get(key));
                } else if (key.equals("采集地点")) {
                    pondId = list.get(i).get(key);
                } else {
                    date = simpleDateFormat.parse(list.get(i).get(key));
                }
            }
            FishPond fishPond = fishPondMapper.selectById(pondId);
            WeatherFactor weatherFactor = new WeatherFactor(temperature,windSpeed,airPressure,illumination,humidity,date,fishPond);
            if (weatherFactorMapper.selectIdByPondIdAndCollectionTime(weatherFactor) == null) {
                weatherFactorMapper.insertWeatherFactor(weatherFactor);
            }else{
                weatherFactorMapper.updateWeatherFactor(weatherFactor);
            }
        }
    }

    public List<WeatherFactor> getByPondId(String pondId) {
        return weatherFactorMapper.getByPondId(pondId);
    }

    public List<String> getPondId() {
        return weatherFactorMapper.getPondId();
    }

    public List<WeatherFactor> getWeatherInfo(String el) {
        return weatherFactorMapper.getWeatherInfo(el);
    }

    public List<WeatherFactor> getByPondIdAndDate(Map<String, Object> map) {
        return weatherFactorMapper.getByPondIdAndDate(map);
    }

    public List<WeatherFactor> getWeatherInfoByDate(Date date) {
        return weatherFactorMapper.getWeatherInfoByDate(date);
    }
}
